package com.dp.temp;

import com.dp.poi.ReadAndCreateExcel;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;


@Component
@RestController
@RequestMapping("/AAA")
public class getHdPlainListMJ {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    RedisTemplate redisTemplate;


    @GetMapping(value = "/AAA")
    public void wordHdPcRw2(HttpServletResponse response) {
        try {
            Workbook workbook = hdPcRw2(A(), "document/aaaa.xlsx");
            OutputStream out = response.getOutputStream();
            response.setHeader("Content-Type", "application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=风险管控报告.xlsx");
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Map<String, Object>> A(){
        return jdbcTemplate.queryForList(" SELECT ONAME,RISK_UNIT_NAME,RISK_EVENT_NAME,RISKGRADE,CLASSIFY1,CLASSIFY2,CLASSIFY3,CCONTENT,a.TROUBLESHOOT_CONTENT,UNAMELIST,HECK_CYCL,CHECK_CYCLE_UNIT,a.WORK_DAY_TYPE,IFSTAR  FROM hd_check_plain a left join risk_control b on a.RISK_MEASURE_ID = b.RID WHERE a.DELETED = '0' and a.WORK_TYPE = '0' ");
    }


    public static Workbook hdPcRw2(List<Map<String, Object>> map, String path) throws IOException {
        Resource resource = new ClassPathResource(path);
        Workbook workbook = new XSSFWorkbook(resource.getInputStream());
        int count = 3;
        int rcount = 2;
        for (int i = 0; i < map.size(); i++) {
            if (i != map.size() - 1) {
                ReadAndCreateExcel.copyRows(1, 1, count, ((XSSFWorkbook) workbook).getSheetAt(0));
            }
            Row row = workbook.getSheetAt(0).getRow(rcount);
            if (row != null) {
                row.getCell(0).setCellValue(i + 1);
                row.getCell(1).setCellValue(map.get(i).get("ONAME").toString());
                row.getCell(2).setCellValue(map.get(i).get("RISK_UNIT_NAME").toString());
                row.getCell(3).setCellValue(map.get(i).get("RISK_EVENT_NAME").toString());
                switch (Integer.parseInt(map.get(i).get("RISKGRADE").toString())){
                    case 1:
                        row.getCell(4).setCellValue("工程技术");
                        break;
                    case 2:
                        row.getCell(4).setCellValue("维护保养");
                        break;
                    case 3:
                        row.getCell(4).setCellValue("操作行为");
                        break;
                    case 4:
                        row.getCell(4).setCellValue("应急措施");
                        break;
                }
                switch (Integer.parseInt(map.get(i).get("CLASSIFY1").toString())){
                    case 1:
                        row.getCell(5).setCellValue("重大风险");
                        break;
                    case 2:
                        row.getCell(5).setCellValue("较大风险");
                        break;
                    case 3:
                        row.getCell(5).setCellValue("一般风险");
                        break;
                    case 4:
                        row.getCell(5).setCellValue("低风险");
                        break;
                }
                switch (map.get(i).get("CLASSIFY2").toString()){
                    case "1-1":
                        row.getCell(6).setCellValue("工艺控制");
                        break;
                    case "1-2":
                        row.getCell(6).setCellValue("关键设备/部件");
                        break;
                    case "1-3":
                        row.getCell(6).setCellValue("安全附件");
                        break;
                    case "1-4":
                        row.getCell(6).setCellValue("安全仪表");
                        break;
                    case "1-5":
                        row.getCell(6).setCellValue("其他");
                        break;
                    case "2-1":
                        row.getCell(6).setCellValue("动设备");
                        break;
                    case "2-2":
                        row.getCell(6).setCellValue("静设备");
                        break;
                    case "3-1":
                        row.getCell(6).setCellValue("人员资质");
                        break;
                    case "3-2":
                        row.getCell(6).setCellValue("操作记录");
                        break;
                    case "3-3":
                        row.getCell(6).setCellValue("交接班");
                        break;
                    case "4-1":
                        row.getCell(6).setCellValue("应急设施");
                        break;
                    case "4-2":
                        row.getCell(6).setCellValue("个体防护");
                        break;
                    case "4-3":
                        row.getCell(6).setCellValue("消防设施");
                        break;
                    case "4-4":
                        row.getCell(6).setCellValue("应急预案");
                        break;
                }
                row.getCell(7).setCellValue(map.get(i).get("CLASSIFY3").toString());
                row.getCell(8).setCellValue(map.get(i).get("CCONTENT").toString());
                row.getCell(9).setCellValue(map.get(i).get("TROUBLESHOOT_CONTENT").toString());
                row.getCell(10).setCellValue(map.get(i).get("UNAMELIST").toString());
                row.getCell(11).setCellValue(map.get(i).get("HECK_CYCL").toString());
                row.getCell(12).setCellValue(map.get(i).get("CHECK_CYCLE_UNIT").toString());
                row.getCell(13).setCellValue("每天");
                if (Integer.parseInt(map.get(i).get("IFSTAR").toString()) == 0) {
                    row.getCell(14).setCellValue("是");
                } else {
                    row.getCell(14).setCellValue("否");
                }
            }
            rcount++;
            count++;
        }
        return workbook;
    }

}
